package com.dubbo.controller;

import com.dubbo.common.check.CheckUtil;
import com.dubbo.common.redis.RedisDao;
import com.dubbo.common.result.ResultInfo;
import com.dubbo.common.security.SecurityUtils;
import com.dubbo.domain.User;
import com.dubbo.service.business.placeOrder.PlaceOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 下单Controller
 * @author wujunyuan
 * @date 2018年12月15日  上午11:03:26
 */
@Controller
@RequestMapping("/placeOrder")
public class PlaceOrderController {
	@Autowired
	private PlaceOrderService placeOrderService;
	@Autowired
	private RedisDao redisDao;
	/**
	 * 获取下单首页一级系列数据
	 * @author wujunyuan
	 * @date 2018年12月26日  下午5:21:23
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/findCategory")
	@ResponseBody
	public Map<String,Object> findCategory(HttpServletRequest request) throws Exception{
		
		return placeOrderService.findCategoryPlaceOrder();
	}
	
	/**
	 * 根据一级系列id获取下单首页二级系列数据
	 * @author wujunyuan
	 * @date 2019年1月14日  上午10:40:37
	 * @param request
	 * @param categoryId
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/findCategory2")
	@ResponseBody
	public Map<String,Object> findCategory2(HttpServletRequest request,Long categoryId) throws Exception{
		
		return placeOrderService.findCategoryPlaceOrder2(categoryId);
	}
	
	/**
	 * 获取下单首页商品数据
	 * @author wujunyuan
	 * @date 2018年12月26日  下午5:21:42
	 * @param request
	 * @param categoryId
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/findGoods")
	@ResponseBody
	public Map<String,Object> findGoods(HttpServletRequest request,Long categoryId,Long pageStart,Long pageTotal){
		//验证是否登录
		User user;
		try {
			user = (User) CheckUtil.checkOline3(request);
			return placeOrderService.findGoodsPlaceOrder(user,categoryId,pageStart,pageTotal);
		} catch (Exception e) {
			user = null;
			return placeOrderService.findGoodsPlaceOrder(user,categoryId,pageStart,pageTotal);
		}
		
	}
	
	
	/**
	 * 添加进购物车
	 * @author wujunyuan
	 * @date 2018年12月17日  上午11:58:51
	 * @param request
	 * @param shoppingcartJson 购物车json数据
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("/addShoppingcart")
	@ResponseBody
	public Map<String,Object> addShoppingcart(HttpServletRequest request,String shoppingcartJson) throws Exception{
		//验证是否登录
		User user = (User) CheckUtil.checkOline3(request);
		SecurityUtils.checkPermission("sys:shoppingcart:add", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return placeOrderService.addShoppingcart(shoppingcartJson,user.getId());
	}
	
	/**
	 * 查看购物车
	 * @author wujunyuan
	 * @date 2018年12月17日  下午3:11:40
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/findShoppingcartList")
	@ResponseBody
	public Map<String,Object> findShoppingcartList(HttpServletRequest request,Long pageStart,Long pageTotal) throws Exception{
		// 验证是否登录
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:shoppingcart:show", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return placeOrderService.findShoppingcartList(pageStart, pageTotal, user.getId());
	}
	
	/**
	 * 修改购物车商品数量
	 * @author wujunyuan
	 * @date 2018年12月20日  下午2:37:26
	 * @param request
	 * @param shoppingcartId 购物车id
	 * @param count 商品数量
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/editShoppingcarCount")
	@ResponseBody
	public Map<String,Object> editShoppingcarCount(HttpServletRequest request,Long shoppingcartId,Integer count) throws Exception{
		// 验证是否登录
		CheckUtil.checkOline3(request);
		//校验权限
//		SecurityUtils.checkPermission("sys:shoppingcart:edit", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return placeOrderService.editShoppingcarCount(shoppingcartId,count);
	}
	
	/**
	 * 移除出购物车
	 * @author wujunyuan
	 * @date 2018年12月17日  下午4:23:03
	 * @param request
	 * @param ids 购物车id数组
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/removeShoppingcart")
	@ResponseBody
	public Map<String,Object> removeShoppingcart(HttpServletRequest request,String idJson) throws Exception{
		//验证是否登录
		User user = (User)CheckUtil.checkOline3(request);
		//校验权限
//		SecurityUtils.checkPermission("sys:shoppingcart:del", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return placeOrderService.removeShoppingcart(user,idJson);
	}
	
	
	/**
	 * 结算
	 * @author wujunyuan
	 * @date 2018年12月19日  下午2:18:36
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/settlement")
	@ResponseBody
	public Map<String, Object> settlement(HttpServletRequest request) throws Exception {
		// 验证是否登录
		User user = (User) CheckUtil.checkOline3(request);
		SecurityUtils.checkPermission("sys:shoppingcart:settlement", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return placeOrderService.settlement(user);
	}
	
	/**
	 * 提交订单
	 * @author wujunyuan
	 * @date 2018年12月18日  上午10:00:47
	 * @param request
	 * @param orderJson 订单json数据
	 * @param shoppingcartIds 购物车id数组
	 * @param electricianId 关联人id(一般为电工)
	 * @return 
	 * @throws Exception
	 */
	@PostMapping("/submitOrder")
	@ResponseBody
	public Map<String, Object> submitOrder(HttpServletRequest request,String orderJson,
			String shoppingcartIds,String shoppingJson,String electricianId) throws Exception {
		// 验证是否登录
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:order:add", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return placeOrderService.submitOrder(user,orderJson,shoppingcartIds,shoppingJson,electricianId);
	}
	
	
	/**
	 * 修改支付方式
	 * @author wujunyuan
	 * @date 2018年12月20日  上午9:49:33
	 * @param request
	 * @param orderNum 订单编号
	 * @param payTape 支付类型1=微信 2=支付宝
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/choosePayment")
	@ResponseBody
	public Map<String, Object> choosePayment(HttpServletRequest request,String orderNum,Integer payType) throws Exception {
		// 验证是否登录
		CheckUtil.checkOline3(request);
		//校验权限
//		SecurityUtils.checkPermission("sys:order:pay", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return placeOrderService.choosePayment(orderNum,payType);
	}
	
	/**
	 * 统计购物车商品数量和金额
	 * @author chenluqiang
	 * @date 2019年3月19日 下午4:50:14 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/countShoppingCartMoney")
	@ResponseBody
	public Map<String, Object> countShoppingCartMoney(HttpServletRequest request) throws Exception {
		// 验证是否登录
		User user = (User) CheckUtil.checkOline3(request);
		return placeOrderService.countShoppingCartMoney(user);
	}
	
	
}
